/**
 * WebSocket管理器模块
 * 负责WebSocket连接和消息处理
 */

class SocketManager {
    constructor() {
        this.socket = null;
        this.init();
    }

    init() {
        this.connect();
        this.setupEventHandlers();
    }

    connect() {
        // this.socket = io("http://172.16.250.234:5000");  #修改 6/13
        this.socket = io("http://172.16.250.167:5000");
    }

    setupEventHandlers() {
        if (!this.socket) return;

        this.socket.on('connect', () => {
            window.logManager?.addLogEntry('info', 'WebSocket连接已建立');
        });

        this.socket.on('disconnect', () => {
            window.logManager?.addLogEntry('error', 'WebSocket连接已断开');
        });

        this.socket.on('system_status_changed', (data) => {
            window.systemManager?.updateSystemStatus(data.running);
        });

        this.socket.on('detection_result', (result) => {
            window.detectionManager?.processDetectionResult(result);
        });

        this.socket.on('statistics_update', (stats) => {
            window.statisticsManager?.processStatisticsUpdate(stats);
        });

        this.socket.on('voice_response', (data) => {
            window.logManager?.addLogEntry('info', `语音响应: ${data.message}`);
        });
    }

    emit(event, data) {
        if (this.socket) {
            this.socket.emit(event, data);
        }
    }

    disconnect() {
        if (this.socket) {
            this.socket.disconnect();
        }
    }
}

// 导出到全局
window.SocketManager = SocketManager; 